package com.sky.utils;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 图片上传 阿里云OSS云服务工具类
 */
@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class AliyunUploadImgUtil {

    // OSS域名
    private String endpoint;
    // OSS accessKeyId
    private String accessKeyId;
    // OSS accessKeySecret
    private String accessKeySecret;
    // OSS bucket名
    private String bucketName;
    // 本项目名称
    private String projectName;


    /**
     *  图片上传   阿里云OSS云服务
     * @param inputStream    文件IO流
     * @param suffix         文件后缀名 .jpg   .png
     */
    public String uploadImaToOSS(InputStream inputStream, String suffix){

        //返回的图片访问路径
        String url = null;

        // 1.拼接文件在OSS中完整的的路径
        String fileName = projectName + "/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date()).toString() +
                "/" + UUID.randomUUID().toString().replace("-","") + suffix;

        // 2.创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            //拼接图片访问路径  "http://"在yml中添加的的
            // https:// bucketName . 域名 / 项目名 /文件名
            // url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
            url = "https://" + bucketName + "." + endpoint + "/" + fileName;

            log.info("文件上传到--{}", url);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return url;
    }



}
